# 第一阶段：使用官方 Go 镜像进行构建
FROM golang:1.24 AS builder

# 设置时区和语言环境（构建阶段可选）
ENV TZ Asia/Shanghai 
ENV LANG C.UTF-8 

# 设置工作目录
WORKDIR /app

# 复制源代码
COPY . .

# 下载依赖
RUN  go env -w GOPROXY=https://goproxy.cn,direct && \ 
     go mod download


# 构建应用
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o ai-collections ./cmd/server

# 第二阶段
# 运行时镜像
FROM openeuler/openeuler:24.03-lts


# 设置时区为亚洲/上海
ENV TZ Asia/Shanghai 
ENV LANG C.UTF-8


# 创建必要的目录
RUN mkdir -p /app/static /app/data /app/config /app/logs



# 设置工作目录
WORKDIR /app

# 从构建阶段复制二进制文件
COPY --from=builder  /app/ai-collections /app/
COPY --from=builder  /app/static /app/static
COPY --from=builder  /app/data /app/data
COPY --from=builder  /app/pkg/config/config.json /app/config/

# 设置必要的权限
RUN chmod +x /app/ && \
    chmod -R 777 /app/logs
# 暴露端口
EXPOSE 8081

# 设置健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD wget -qO- http://localhost:8081/ai-collections/health || exit 1

# 运行应用
ENTRYPOINT ["/app/ai-collections", "-config=/app/config/config.json"]